package com.chengqj.blogweb.blog.service.impl;

import com.chengqj.blogweb.blog.repo.dao.TBlogMapper;
import com.chengqj.blogweb.blog.repo.dao.TBlogPhotoMapper;
import com.chengqj.blogweb.blog.repo.dao.TBlogRecommendMapper;
import com.chengqj.blogweb.blog.repo.pojo.TBlog;
import com.chengqj.blogweb.blog.repo.pojo.TBlogPhoto;
import com.chengqj.blogweb.blog.repo.pojo.TBlogRecommend;
import com.chengqj.blogweb.blog.service.IBlogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: blog-web
 * @description: 文章内容查询
 * @author: chengqj
 * @create: 2018-08-13 10:59
 **/
@Service
public class BlogServiceImpl implements IBlogService {
    @Autowired
    private TBlogMapper blogMapper;
    @Autowired
    private TBlogRecommendMapper recommendMapper;
    @Autowired
    private TBlogPhotoMapper blogPhotoMapper;

    @Override
    public PageInfo<TBlogPhoto> selectBlogPhotoByType(String type, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<TBlogPhoto> resultList = null;
        if (StringUtil.isEmpty(type)) {
            resultList = blogPhotoMapper.selectAll();
        } else {
            resultList = blogPhotoMapper.selectByType(type);
        }
        PageInfo<TBlogPhoto> appsPageInfo = new PageInfo<>(resultList);
        return appsPageInfo;
    }

    @Override
    public PageInfo<TBlogPhoto> selectBlogPhotoByMain() {
        List<TBlogPhoto> resultList = blogPhotoMapper.selectByMain();
        PageInfo<TBlogPhoto> appsPageInfo = new PageInfo<>(resultList);
        return appsPageInfo;
    }

    @Override
    public PageInfo<TBlog> selectBlogCriterion(String type, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<TBlog> resultList = null;
        if (StringUtil.isEmpty(type)){
            resultList = blogMapper.selectAll();
        }else{
            if ("1".equals(type)){
                resultList = blogMapper.selectByType("设计模式");
            }
            if ("2".equals(type)){
                resultList = blogMapper.selectByType("spring");
            }
            if ("3".equals(type)){
                resultList = blogMapper.selectByType("java");
            }
            if ("4".equals(type)){
                resultList = blogMapper.selectByType("缓存");
            }
            if ("5".equals(type)){
                resultList = blogMapper.selectByType("分布式");
            }
            if ("6".equals(type)){
                resultList = blogMapper.selectByType("spring cloud");
            }
            if ("7".equals(type)){
                resultList = blogMapper.selectByType("spring boot");
            }
            if ("8".equals(type)){
                resultList = blogMapper.selectByType("性能");
            }
        }
        PageInfo<TBlog> appsPageInfo = new PageInfo<>(resultList);
        return appsPageInfo;
    }

    public List<TBlogRecommend> selectRecommendAll() {
        return recommendMapper.selectAll();
    }

    @Override
    public TBlog selectBlogById(int id) {
        return blogMapper.selectByPrimaryKey(id);
    }

}
